package java2020spring;

import javax.swing.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.text.BadLocationException;
import javax.swing.undo.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class EditorMenu extends JFrame implements ActionListener,CaretListener{
	
	JMenuBar menubar;    //创建菜单条
	JMenu menu_File,menu_Edit,menu_Format,menu_View,menu_Help;    //创建各个菜单
	JMenuItem item_New,item_Open,item_Save,item_SaveAs,item_Exit;    //创建File菜单的菜单项
	JMenuItem item_Undo,item_Cut,item_Copy,item_Stick,item_Delete,item_All;    //创建Edit菜单的菜单项
	JMenuItem item_Wrap;    //创建Format菜单的菜单项
	JMenuItem item_StatusBar;    //创建View菜单的菜单项
	JMenuItem item_ViewHelp,item_About;    //创建Help菜单的菜单项
	
	JTextArea textarea=new JTextArea(50,150);        //创建文本区
	JScrollPane scroll=new JScrollPane(textarea);    //把文本区放到一个滚动窗格中
	JLabel statuslabel=new JLabel("当前字数:"+0+"   第"+0+"行"+"   第"+0+"列");	
	UndoManager undo=new UndoManager();
	
	String name;
	public EditorMenu(){}
	public EditorMenu(String s){
		this.init(s);
		this.setTitle(s);
		Container con=this.getContentPane();
		con.setBackground(Color.WHITE);    //设置窗口背景色
		this.pack();    //设置窗口大小为最佳
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);    //释放当前窗口
	}
	
	private void init(String s) {
		menubar=new JMenuBar();
		
		menu_File=new JMenu("文件(F)");
		item_New=new JMenuItem("新建(N)");
		item_Open=new JMenuItem("打开(O)");
		item_Save=new JMenuItem("保存(S)");
		item_SaveAs=new JMenuItem("另存为(A)");
		item_Exit=new JMenuItem("退出(X)");
		
		menu_Edit=new JMenu("编辑(E)");
		item_Undo=new JMenuItem("撤销(U)");
		item_Cut=new JMenuItem("剪切(T)");
		item_Copy=new JMenuItem("复制(C)");
		item_Stick=new JMenuItem("粘贴(P)");
		item_Delete=new JMenuItem("删除(L)");
		item_All=new JMenuItem("全选(A)");
		
		menu_Format=new JMenu("格式(O)");
		item_Wrap=new JCheckBoxMenuItem("自动换行(W)");
		
		menu_View=new JMenu("查看(V)");
		item_StatusBar=new JCheckBoxMenuItem("状态栏(S)");
		
		menu_Help=new JMenu("帮助(H)");
		item_ViewHelp=new JMenuItem("查看帮助");
		item_About=new JMenuItem("关于记事本");
		
		menu_File.add(item_New);
		menu_File.add(item_Open);
		menu_File.add(item_Save);
		menu_File.add(item_SaveAs);
		menu_File.add(item_Exit);
		
		menu_Edit.add(item_Undo);
		menu_Edit.add(item_Cut);
		menu_Edit.add(item_Copy);
		menu_Edit.add(item_Stick);
		menu_Edit.add(item_Delete);
		menu_Edit.add(item_All);
			
		menu_Format.add(item_Wrap);
				
		menu_View.add(item_StatusBar);
		
		menu_Help.add(item_ViewHelp);
		menu_Help.add(item_About);
		
		menubar.add(menu_File);
		menubar.add(menu_Edit);
		menubar.add(menu_Format);
		menubar.add(menu_View);
		menubar.add(menu_Help);	
		
		this.add(scroll);
		this.add(statuslabel,BorderLayout.SOUTH);
		statuslabel.setVisible(false);
		setJMenuBar(menubar);
		
		item_New.addActionListener(this);
		item_Open.addActionListener(this);
		item_Save.addActionListener(this);
		item_SaveAs.addActionListener(this);
		item_Exit.addActionListener(this);
		item_Undo.addActionListener(this);
		item_Cut.addActionListener(this);
		item_Copy.addActionListener(this);
		item_Stick.addActionListener(this);
		item_Delete.addActionListener(this);
		item_All.addActionListener(this);
		item_Wrap.addActionListener(this);
		item_StatusBar.addActionListener(this);
		item_ViewHelp.addActionListener(this);
		item_About.addActionListener(this);
		textarea.addCaretListener(this);//向各个菜单项注册监听器
		
		textarea.getDocument().addUndoableEditListener(undo);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO 自动生成的方法存根
		if(e.getSource()==item_New){
			NewTextEditor window=new NewTextEditor();
		}
		else if(e.getSource()==item_Open){
			openFile();
		}
		else if(e.getSource()==item_Save){
			saveFile();
		}
		else if(e.getSource()==item_SaveAs){
			saveAsFile();
		}
		else if(e.getSource()==item_Exit){
			this.dispose();
		}
		else if(e.getSource()==item_Undo){
		    if (undo.canUndo()) {
		    	try {
		    		undo.undo();
		    	}catch (CannotUndoException k) {
		    		k.printStackTrace();
		    	}
			}
		}
		else if(e.getSource()==item_Cut){
			textarea.cut();
		}
		else if(e.getSource()==item_Copy){
			textarea.copy();
		}
		else if(e.getSource()==item_Stick){
			textarea.paste();
		}
		else if(e.getSource()==item_Delete){
			int start=textarea.getSelectionStart();
			int end=textarea.getSelectionEnd();
			textarea.replaceRange("", start, end);
		}
		else if(e.getSource()==item_All){
			textarea.selectAll();
		}
		else if(e.getSource()==item_Wrap){
			if(textarea.getLineWrap())
				textarea.setLineWrap(false);
			else
				textarea.setLineWrap(true);    //自动换行
			    textarea.setWrapStyleWord(true);    //确保换行不断字
		}
		else if(e.getSource()==item_StatusBar){
			if(((JCheckBoxMenuItem) item_StatusBar).getState())  
                statuslabel.setVisible(true);  
            else   
                statuslabel.setVisible(false);  
        }
		else if(e.getSource()==item_ViewHelp){
			HelpWindow win=new HelpWindow("帮助");
		}
		else if(e.getSource()==item_About){
			AboutWindow win=new AboutWindow("关于");
		}
		
	}

	private void openFile() {
		File file=null;
		int result;
		JFileChooser fileChooser = new JFileChooser("C:\\");
		fileChooser.setApproveButtonToolTipText("打开");
		fileChooser.setDialogTitle("打开文件所在位置");
		result=fileChooser.showOpenDialog(item_Open);
		if(result==JFileChooser.APPROVE_OPTION){
			file=fileChooser.getSelectedFile();
		}
		if(file.isFile()&&file.exists()){
			BufferedReader reader=null;
			try{
				InputStreamReader inputStreamReader=new InputStreamReader(new FileInputStream(file),"UTF-8");
				reader=new BufferedReader(inputStreamReader);
				String readLine="";
				while((readLine=reader.readLine())!=null){
					textarea.append(readLine+'\n');
				}
				reader.close();
			}catch(IOException e){
				e.printStackTrace();
			}
		}
	}
	
	private void saveFile() {
		File file=null;
		int result;
		JFileChooser fileChooser = new JFileChooser("C:\\");
		fileChooser.setApproveButtonToolTipText("保存");
		fileChooser.setDialogTitle("保存文件");
		result=fileChooser.showSaveDialog(item_Save);
		if(result==JFileChooser.APPROVE_OPTION){
			file=fileChooser.getSelectedFile();
		}
		try{
			OutputStreamWriter write=new OutputStreamWriter(new FileOutputStream(file),"UTF-8");
			BufferedWriter writer=new BufferedWriter(write);
			String content=textarea.getText();
			writer.write(content);
			writer.close();
		}catch(IOException e){
			e.printStackTrace();
		}
	}
	
	private void saveAsFile() {
		File file=null;
		int result;
		JFileChooser fileChooser = new JFileChooser("C:\\");
		fileChooser.setApproveButtonToolTipText("保存");
		fileChooser.setDialogTitle("另存为");
		result=fileChooser.showSaveDialog(item_SaveAs);
		if(result==JFileChooser.APPROVE_OPTION){
			file=fileChooser.getSelectedFile();
		}
		try{
			OutputStreamWriter write=new OutputStreamWriter(new FileOutputStream(file),"UTF-8");
			BufferedWriter writer=new BufferedWriter(write);
			String content=textarea.getText();
			writer.write(content);
			writer.close();
		}catch(IOException e){
			e.printStackTrace();
		}
		
	}
	@Override
	public void caretUpdate(CaretEvent e) {
		// TODO 自动生成的方法存根
		int currentNumber;
		int currentRow;
		int currentColumn;
		int p=textarea.getCaretPosition();
		try{
			currentRow=textarea.getLineOfOffset(p)+1;
			currentColumn=p-textarea.getLineStartOffset(currentRow-1)+1;
			String str=textarea.getText().replaceAll("\\s+","");
			currentNumber=str.length();
			statuslabel.setText("当前字数:"+String.valueOf(currentNumber)+"   第"+currentRow+"行"+"   第"+currentColumn+"列");
		}catch(BadLocationException x){
			x.printStackTrace();
		}
	}

	
	
	
}
